package com.yc.chaoshi.cs_manage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.yc.chaoshi.cs_manage.mapper.PmsGoodsMapper;
import com.yc.chaoshi.cs_manage.service.impl.PmsGoodsServiceImpl;
import com.yc.chaoshi.entity.PmsGoods;
import com.yc.chaoshi.entity.UmsUser;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xz
 * @since 2022-08-04
 */
@RestController
@RequestMapping("/chaoshi/pmsGoods")
public class PmsGoodsController {
    @Resource
    private PmsGoodsMapper pmsGoodsMapper;
//    @RequestMapping("queryGoods")
//    public Page queryGoods( @RequestParam(defaultValue = "1") int page){
//        Page<PmsGoods> ipage = new Page<>(page,10);
//        ipage.orders().add(OrderItem.desc("id"));
//        ipage.setRecords(pmsGoodsMapper.queryGoods((page-1)*10));
//        long total = pmsGoodsMapper.selectCount(null);
//        ipage.setTotal(total);
////        return pmsGoodsMapper.queryGoods(ipage);
//        return ipage;
//    }
    @RequestMapping("selectGoods")
    public Page selectGoods( @RequestParam(defaultValue = "") String name,
                             @RequestParam(defaultValue = "0") int oid,
                             @RequestParam(defaultValue = "1") int page){
        Page<PmsGoods> ipage = new Page<>(page,10);
        ipage.orders().add(OrderItem.asc("id"));
        System.out.println(pmsGoodsMapper.selectGoods(name, oid, (page - 1) * 10,10));
        ipage.setRecords(pmsGoodsMapper.selectGoods(name,oid,(page-1)*10,10));
        System.out.println(pmsGoodsMapper.selectGoods(name,oid,0,1000).size());
        long total = pmsGoodsMapper.selectGoods(name,oid,0,1000).size();
        System.out.println(pmsGoodsMapper.selectCount(null));
        ipage.setTotal(total);
        return ipage;
    }

    @RequestMapping("updatedel")
    public int updateDel(@RequestParam int id){
        UpdateWrapper<PmsGoods> updateWrapper=new UpdateWrapper<>();
        updateWrapper.lambda().eq(PmsGoods::getId,id)
                        .set(PmsGoods::getDel,0);
        return pmsGoodsMapper.update(null,updateWrapper);
    }
    @RequestMapping("updateGoods")
    public int updateGoods(@RequestParam int oid, @RequestParam String name,
                           @RequestParam int points, @RequestParam String image,
                           @RequestParam String image_album, @RequestParam int del,
                           @RequestParam int grd, @RequestParam int sgrd,
                           @RequestParam int sales, @RequestParam int stock,
                           @RequestParam double price, @RequestParam double discount_price,
                           @RequestParam int id) {
        pmsGoodsMapper.updateGood(oid, name, points, image, image_album, del,
                grd, sgrd, sales, stock, price, discount_price,id);
        return 0;
    }

    @RequestMapping("insertGoods")
    public int insertGoods(@RequestParam int oid, @RequestParam String name,
                           @RequestParam int points, @RequestParam String image,
                           @RequestParam String image_album, @RequestParam int del,
                           @RequestParam int grd, @RequestParam int sgrd,
                           @RequestParam int sales, @RequestParam int stock,
                           @RequestParam double price, @RequestParam double discount_price){
        pmsGoodsMapper.insertGood(oid, name, points, image, image_album,
                del, grd, sgrd, sales, stock, price, discount_price);
        return 0;
    }
}

